package com.jingdianjichi.interview.server.server.impl;

import com.google.common.base.Preconditions;
import com.jingdianjichi.interview.api.req.InterviewReq;
import com.jingdianjichi.interview.api.req.InterviewSubmitReq;
import com.jingdianjichi.interview.api.req.StartReq;
import com.jingdianjichi.interview.api.vo.InterviewQuestionVO;
import com.jingdianjichi.interview.api.vo.InterviewResultVO;
import com.jingdianjichi.interview.api.vo.InterviewVO;
import com.jingdianjichi.interview.server.entity.po.SubjectLabel;
import com.jingdianjichi.interview.server.mapper.SubjectDao;
import com.jingdianjichi.interview.server.engine.InterviewEngine;
import com.jingdianjichi.interview.server.server.InterviewService;
import com.jingdianjichi.interview.server.utils.PDFUtil;
import com.jingdianjichi.interview.server.utils.keyword.KeyWordUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class InterviewServiceImpl implements InterviewService, ApplicationContextAware {

    //引擎
    private static final Map<String, InterviewEngine> engineMap = new HashMap<>();

    @Resource
    private SubjectDao subjectLabelDao;

    //使用bean的声明周期实现引擎的初始化

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //获得实现InterviewEngine接口的引擎实体类
        Collection<InterviewEngine> engines = applicationContext.getBeansOfType(InterviewEngine.class).values();
        for (InterviewEngine engine : engines) {
            engineMap.put(engine.engineType().name(), engine);
        }
    }

    //简历分析,主要用的是关键词
    @Override
    public InterviewVO analyse(InterviewReq req) {
        //构建关键词,做pdf识别
        List<String> keyWords = buildKeyWords(req.getUrl());

        //获得引擎
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkArgument(!Objects.isNull(engine), "引擎不能为空！");

        //分析
        return engine.analyse(keyWords);
    }

    //获得模拟面试
    @Override
    public InterviewQuestionVO start(StartReq req) {
        //获得引擎
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkArgument(!Objects.isNull(engine), "引擎不能为空！");

        return engine.start(req);
    }

    //提交面试题
    @Override
    public InterviewResultVO submit(InterviewSubmitReq req) {
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkArgument(!Objects.isNull(engine), "引擎不能为空！");

        return engine.submit(req);
    }


    //构造关键词,使用pdf工具
    private List<String> buildKeyWords(String url) {
        //获得文档内容
        String pdfText = PDFUtil.getPdfText(url);

        //对于pdf文字的识别我们还是使用dfa算法实现,这个工具类就是这样的奥
        if (!KeyWordUtil.isInit()) {
            //如果我们的标签名字没有初始化,我们就给他初始化
            List<String> list = subjectLabelDao.listAllLabel().stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
            KeyWordUtil.addWord(list);
        }

        //这个方法就是把符合我们标签的词,放进来,这样你就知道简历中有啥标签了
        return KeyWordUtil.buildKeyWordsLists(pdfText);
    }

}
